import json
from random import randrange

import requests
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import render

from jinja2 import Environment, FileSystemLoader
from pyecharts.components import Table
from pyecharts.globals import CurrentConfig
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.options import ComponentTitleOpts


def login_view(request):
    if request.method == "GET":
        return render(request, "login.html")
    elif request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if not user:
            return HttpResponse("---密码错误—--")
        else:
            login(request, user)
            if user.is_superuser == True:
                return render(request, 'superuser.html')
            else:
                return HttpResponseRedirect('/')


def register_view(request):
    if request.method == "GET":
        return render(request, "register.html")
    elif request.method == "POST":
        username = request.POST['username']
        password_1 = request.POST['password_1']
        password_2 = request.POST["password_2"]
        if password_1 != password_2:
            return HttpResponse('---两次密码输入不一致---')

        user = User.objects.create_user(username=username, password=password_1)
        return HttpResponseRedirect('/login')


def logout_view(request):
    logout(request)
    return HttpResponse("---已退出----")


# @login_required
# def index_view(request):
#     html="<h1>首页</h1>"
#     return HttpResponse(html)

@login_required
def index_view(request):
    c = (
        Bar()
            .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
            .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
            .add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
            .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
    )
    return HttpResponse(c.render_embed())





def arrangement_table(request):
    datas = '''
    {
        "station_name":"广氮南",
        "date":"2021-10-10"
    }
    '''.encode('utf-8')
    url = "http://81.68.207.71:5000/arrangement_table"
    content = requests.post(url, data=datas).content
    contentJson = json.loads(content)
    table = Table()

    headers = contentJson["headers"]
    rows = ["rows"]
    table.add(headers, rows)
    table.set_global_opts(
        title_opts=ComponentTitleOpts(title=contentJson["title"], subtitle=contentJson["subtitle"])
    )
    return HttpResponse(table.render_embed())


def cars_info_table(request):
    content = requests.get("http://81.68.207.71:5000/cars_info_table").content
    contentJson = json.loads(content)
    table = Table()

    headers = contentJson["headers"]
    rows = ["rows"]
    table.add(headers, rows)
    table.set_global_opts(
        title_opts=ComponentTitleOpts(title=contentJson["title"], subtitle=contentJson["subtitle"])
    )
    return HttpResponse(table.render_embed())
    return HttpResponse(table)


def sales_per_month_bar(request):
    return render(request, "sales_per_month_bar.html")
